#include <thread>
#include <chrono>
#include <atomic>
#include <functional>
#include <iostream>
class Timer {
private:
    std::atomic<bool> running{false};
    std::thread timer_thread;
    std::function<void()> callback;
    std::chrono::milliseconds interval;

public:
    Timer(std::function<void()> cb, std::chrono::milliseconds ms) 
        : callback(cb), interval(ms) {}
    
    void start() {
        running = true;
        timer_thread = std::thread([this]() {
            while (running) {
                std::this_thread::sleep_for(interval);
                if (running) {
                    callback();
                }
            }
        });
    }
    
    void stop() {
        running = false;
        if (timer_thread.joinable()) {
            timer_thread.join();
        }
    }
};

// 使用示例
int main() {
    Timer timer([]() {
        std::cout << "Timer fired!" << std::endl;
    }, std::chrono::seconds(1));
    
    timer.start();
    std::this_thread::sleep_for(std::chrono::seconds(5));
    timer.stop();
    return 0;
}